package com.kavian.probability.collect.site;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LabelTag;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;

import com.kavian.common.util.HttpClientUtil;
import com.kavian.probability.collect.domain.ColorBall;

/**
 * URL：http://www.3dcp.cn/zs/gonggao.php?type=ssq<br />
 * 双色球
 * 
 * @author kavian
 * 
 */
public class CollectColorBall {
	private final String URL = "http://www.gdfc.org.cn/play_list_game_5.html";
	private final String CHARSET = "gb2312";

	/**
	 * 获取分页URL
	 * 
	 * @return
	 */
	public List<String> searchUrl() {
		List<String> urls = new ArrayList<String>();
		try {
			String content = HttpClientUtil.httpGet(URL, CHARSET);

			if (content == null) {
				return null;
			}

			Parser parser = Parser.createParser(content, CHARSET);
			NodeFilter labelFilter = new AndFilter(new NodeClassFilter(
					LabelTag.class), new HasAttributeFilter("id",
					"label-totalpage"));
			NodeList labelList = parser.extractAllNodesThatMatch(labelFilter);

			if (labelList.size() > 0) {

				String total = labelList.elementAt(0).toPlainTextString()
						.trim();
				int pageSize = Integer.parseInt(total);

				for (int i = 1; i <= pageSize; i++) {
					String page = "http://www.gdfc.org.cn/datas/history/twocolorball/history_"
							+ i + ".html";
					urls.add(page);
				}

				return urls;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public List<ColorBall> searchContent(final String url) {
		List<ColorBall> colorBalls = new ArrayList<ColorBall>();
		try {
			String content = HttpClientUtil.httpGet(url, CHARSET);

			Parser parser = Parser.createParser(content, CHARSET);
			NodeFilter tableFilter = new NodeClassFilter(TableTag.class);
			NodeList tableList = parser.extractAllNodesThatMatch(tableFilter);

			if (tableList.size() > 0) {
				TableTag table = (TableTag) tableList.elementAt(0);
				TableRow[] tr = table.getRows();

				if (tr.length > 0) {
					ColorBall colorBall = null;
					for (int i = 1; i < tr.length; i++) {
						colorBall = new ColorBall();
						TableColumn[] td = tr[i].getColumns();

						// 期号
						String issue = td[0].toPlainTextString().trim();
						colorBall.setIssue(issue);

						// 号码
						String ball = td[1].getAttribute("luckyno").trim();
						String[] balls = this.subString(ball, 2);

						String redBall = balls[0] + " " + balls[1] + " "
								+ balls[2] + " " + balls[3] + " " + balls[4]
								+ " " + balls[5];
						String blueBall = balls[balls.length - 1];
						
						colorBall.setLotteryResultsBlue(blueBall);
						colorBall.setLotteryResultsRed(redBall);
						
						//System.out.println(redBall+"  "+blueBall);
						
						colorBalls.add(colorBall);
					}
				}

			}
			return colorBalls;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 按长度拆分字符串
	 * 
	 * @param s
	 *            字符串
	 * @param j
	 *            拆分长度
	 * @return
	 */
	private String[] subString(String s, int j) {
		String[] str = new String[(s.length()/2)];
		int k = 0;
		for (int i = 0; i < s.length(); i = i + j) {
			String a = s.substring(i, i + j);
			str[k] = a;
			k++;
		}

		return str;
	}
}